package caiwei.android.controls;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.RelativeLayout;
import caiwei.android.R;
import caiwei.android.adapter.AdapterSlideMenuList;

/**
 * 自定义滑动菜单类，控制滑动菜单的显示
 * 
 * @author caiwei
 * @date2012-3-5
 * @version 1.0
 */
public class SlideMenuView
{
	private Activity mActivity;
	private List<SlideMenuItem> mMenuList;
	private boolean mIsClosed;
	private RelativeLayout mRelativeLayout;
	private OnSlideMenuListener mOnSlideMenuListener;

	public SlideMenuView(Activity pActivity)
	{
		mActivity = pActivity;
		initView();
		if (pActivity instanceof OnSlideMenuListener)
		{
			mOnSlideMenuListener = (OnSlideMenuListener) pActivity;
			initVarible();
			initListeners();
		}
	}

	/**
	 * 接口，监听器，让使用类去实现
	 * 
	 * @author caiwei
	 * @date2012-3-5
	 * @version 1.0
	 */
	public interface OnSlideMenuListener
	{
		public abstract void onSlideMenuItemClick(View pView,
				SlideMenuItem pSlideMenuItem);
	}

	/**
	 * 初始化View，主布局底部的bottom布局
	 */
	private void initView()
	{
		mRelativeLayout = (RelativeLayout) mActivity
				.findViewById(R.id.IncludeMainBottom);
	}

	/**
	 * 初始化变量
	 */
	private void initVarible()
	{
		mMenuList = new ArrayList<SlideMenuItem>();
		mIsClosed = true;
	}

	/**
	 * 初始化监听器
	 */
	public void initListeners()
	{
		mRelativeLayout.setOnClickListener(new OnSlideMenuClick());
		mRelativeLayout.setFocusableInTouchMode(true);
		mRelativeLayout.setOnKeyListener(new OnKeyListener()
		{

			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event)
			{

				if (keyCode == KeyEvent.KEYCODE_MENU
						&& event.getAction() == KeyEvent.ACTION_UP)
				{
					toggle();
				}

				return false;
			}
		});
	}

	/**
	 * 滑动菜单点击监听器
	 * 
	 * @author caiwei
	 * @date2012-3-5
	 * @version 1.0
	 */
	class OnSlideMenuClick implements OnClickListener
	{

		@Override
		public void onClick(View v)
		{
			toggle();
		}

	}

	/**
	 * 对外提供滑动菜单打开关闭方法
	 */
	public void toggle()
	{
		if (mIsClosed)
		{
			open();
		} else
		{
			close();
		}
	}

	/**
	 * 滑动菜单关闭方法
	 */
	private void close()
	{
		RelativeLayout.LayoutParams _LayoutParams = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.FILL_PARENT, 68);
		_LayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
		mRelativeLayout.setLayoutParams(_LayoutParams);
		mIsClosed = true;
	}

	/**
	 * 滑动菜单打开方法
	 */
	private void open()
	{
		RelativeLayout.LayoutParams _LayoutParams = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.FILL_PARENT,
				RelativeLayout.LayoutParams.FILL_PARENT);
		_LayoutParams.addRule(RelativeLayout.BELOW, R.id.IncludeMainTop);
		mRelativeLayout.setLayoutParams(_LayoutParams);
		mIsClosed = false;
	}

	/**
	 * 对外提供方法添加ListView项给ListView
	 * 
	 * @param pSliderMenuItem
	 */
	public void add(SlideMenuItem pSliderMenuItem)
	{
		mMenuList.add(pSliderMenuItem);
	}

	/**
	 * 对外提供ListView数据绑定方法
	 */
	public void bindList()
	{
		AdapterSlideMenuList _AdapterSlideMenuList = new AdapterSlideMenuList(
				mActivity, mMenuList);
		ListView _ListView = (ListView) mActivity
				.findViewById(R.id.lvSlideList);
		_ListView.setAdapter(_AdapterSlideMenuList);
		_ListView.setOnItemClickListener(new OnSlideMenuItemClick());
	}

	/**
	 * 滑动菜单项点击监听器
	 * 
	 * @author caiwei
	 * @date2012-3-5
	 * @version 1.0
	 */
	private class OnSlideMenuItemClick implements OnItemClickListener
	{

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id)
		{
			SlideMenuItem _SlideMenuItem = (SlideMenuItem) parent
					.getItemAtPosition(position);
			mOnSlideMenuListener.onSlideMenuItemClick(view, _SlideMenuItem);
		}

	}
	/**
	 * 移除底边框
	 */
	public void removeBottomBox()
	{
		RelativeLayout _MainLayout = (RelativeLayout) mActivity
				.findViewById(R.id.layMainLayout);
		_MainLayout.removeView(mRelativeLayout);
		mRelativeLayout = null;
	}
}
